var clientLoginAndRegister=angular.module("clientLoginAndRegister",[]);
clientLoginAndRegister.controller("LoginController",function($scope,$http,$timeout){
    $scope.cu={};
    $scope.loginErro="";
    $scope.loginSubmit=function(){
        if(!$scope.cu.cname||!$scope.cu.cpassword){
            $scope.loginErro="User name or password can not be empty!";
            $timeout(function () {
                $scope.loginErro='';
            }, 3000)
        }
        else{
            var req={
                method:"POST",
                url:"clients/login",
                data:$scope.cu,
                heanders:{"Content-type":"application/json"}
            };
            $http(req)
                .then(function(res){
                    if(res.data.msg=="SUCCESS"){
                        $scope.cu.cname="";
                        $scope.cu.cpassword="";
                        $scope.loginErro="";
                        window.location.href="/"
                    }
                    else{
                        $scope.loginErro="User name or password is incorrect!";
                        $timeout(function () {
                            $scope.loginErro='';
                        }, 3000)
                    }
                })
        }
    }

});
clientLoginAndRegister.controller("RegisterController",function($scope,$http, $timeout){
    $scope.cu={};
    $scope.cnameError="";
    $scope.ctelError="";
    $scope.caddressError="";
    $scope.passwordError="";
    $scope.cpasswordpwdError="";
    var checkCNameB=false;
    $scope.checkCName=function(){
        if(!$scope.cu.cname){
            $scope.cnameError="User name cannot be empty!";
            checkCNameB=false;
        }
        else{
            var req={
                method:"GET",
                url:"/clients/exists/"+$scope.cu.cname
            };
            $http(req)
                .then(function(res){
                    if(res.data.msg=="SUCCESS"){
                        $scope.cnameError="User name already exists!";
                        $timeout(function () {
                            $scope.cnameError="";
                        }, 3000);
                        checkCNameB=false;
                    }
                    else{
                        $scope.cnameError="";
                        checkCNameB=true;
                    }
                })
        }
    }
    $scope.checkCTel=function(){
        if(!$scope.cu.ctel){
            $scope.ctelError="Contact phone number can not be empty!";
            $timeout(function () {
                $scope.ctelError="";
            }, 3000);
            return false;
        }
        else if(!/^(13[0-9]|15[0|1|3|6|7|8|9]|18[8|9])\d{8}$/.test($scope.cu.ctel)){
            $scope.ctelError="Phone format error!";
            $timeout(function () {
                $scope.ctelError="";
            }, 3000);
            return false;
        }
        else{
            $scope.ctelError="";
            return true;
        }
    }
    $scope.checkAddress=function(){
        if(!$scope.cu.address){
            $scope.caddressError="Address can not be empty!";
            $timeout(function () {
                $scope.caddressError="";
            }, 3000);
            return false;
        }
        $scope.caddressError="";
        return true;
    }
    $scope.checkCPassword=function(){
        if(!$scope.cu.password){
            $scope.passwordError="Password can not be empty!";
            $timeout(function () {
                $scope.passwordError="";
            }, 3000);
            return false;
        }
        $scope.passwordError="";
        return true;
    }
    $scope.checkCPasswordpwd=function(){
        if($scope.cu.password1!=$scope.cu.password){
            $scope.cpasswordpwdError="Confirmation password is not identical!";
            $timeout(function () {
                $scope.cpasswordpwdError="";
            }, 3000);
            return false;
        }
        $scope.cpasswordpwdError="";
        return true;
    }
    $scope.registerSubmit=function(){
        console.log("re");
        if($scope.checkCPassword()&&$scope.checkCPasswordpwd()&&checkCNameB&&$scope.checkCTel()&&$scope.checkAddress()){
            var req={
                method:"POST",
                url:"/clients/regist",
                data:$scope.cu,
                headers:{"Content-type":"application/json"}
            };
            $http(req)
                .then(function(res){
                    if(res.data.msg=="SUCCESS"){
                        window.location.href="/login.html"
                    }
                    else{
                        alert("FAILURE");
                    }
                })
        }
    }

});

